{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d04f7c2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "268 ns ± 9.12 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n",
      "73 ns ± 1.59 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "%timeit y=x**2\n",
    "%timeit y=x*x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8036063f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "603 ns ± 47.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n",
      "567 ns ± 6.08 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "z = np.uint8([5])\n",
    "%timeit y=z*z\n",
    "%timeit y=np.square(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4c34456f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.1 µs ± 2.07 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n",
      "14.7 µs ± 1.03 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "img = cv.imread('data/messi5.jpg')\n",
    "img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "%timeit z = cv.countNonZero(img)\n",
    "%timeit z = np.count_nonzero(img)\n",
    "\n",
    "# from https://pynash.org/2013/03/06/timing-and-profiling/\n",
    "# %mprun -f cv.countNonZero cv.countNonZero(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bcd7c287",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
